package com.zhsj.OkeHomeConsumption.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;


import com.bumptech.glide.Glide;
import com.zhsj.OkeHomeConsumption.R;
import com.zhsj.OkeHomeConsumption.empty.UpLoadImgBean;

import java.io.File;
import java.util.List;


/**
 * @描述:添加上传图片适配器
 * @创建人：zhangpeisen
 * @创建时间：2017/5/11 下午10:12
 * @修改人：zhangpeisen
 * @修改时间：2017/5/11 下午10:12
 * @修改备注：
 * @throws
 */
public class GridViewAddImgesAdpter extends BaseAdapter {
    private List<UpLoadImgBean> datas;
    private Context context;
    private LayoutInflater inflater;
    /**
     * 可以动态设置最多上传几张，之后就不显示+号了，用户也无法上传了
     * 默认9张
     */
    private int maxImages = 9;

    public GridViewAddImgesAdpter(List<UpLoadImgBean> datas, Context context) {
        this.datas = datas;
        this.context = context;
        inflater = LayoutInflater.from(context);
    }

    /**
     * 获取最大上传张数
     *
     * @return
     */
    public int getMaxImages() {
        return maxImages;
    }

    /**
     * 设置最大上传张数
     *
     * @param maxImages
     */
    public void setMaxImages(int maxImages) {
        this.maxImages = maxImages;
    }

    /**
     * 让GridView中的数据数目加1最后一个显示+号
     *
     * @return 返回GridView中的数量
     */
    @Override
    public int getCount() {
        int count = datas == null ? 1 : datas.size() + 1;
        if (count >= maxImages) {
            return datas.size();
        } else {
            return count;
        }
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    public void notifyDataSetChanged(List<UpLoadImgBean> datas) {
        this.datas = datas;
        this.notifyDataSetChanged();

    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {


        ViewHolder viewHolder = null;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.onekeyfix_image_grid, parent, false);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        if (datas != null && position < datas.size()) {
            if(position == datas.size()-1&&datas.size()!=1){
                viewHolder.delete_image.setVisibility(View.GONE);
                viewHolder.ivimage.setEnabled(true);
            }else{
                viewHolder.ivimage.setEnabled(false);
            }
            viewHolder.delete_image.setVisibility(View.VISIBLE);
            Glide.with(context).load(datas.get(position).getPhotoPath()).placeholder(R.drawable.zhanweitu_bg).into(viewHolder.ivimage);
        } else {
            viewHolder.delete_image.setVisibility(View.GONE);
            Glide.with(context).load("").placeholder(R.mipmap.tianjia_right).into(viewHolder.ivimage);
            viewHolder.ivimage.setScaleType(ImageView.ScaleType.FIT_XY);
        }
        viewHolder.delete_image.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                    datas.remove(position);
                    notifyDataSetChanged(datas);
            }
        });

        return convertView;

    }

    public class ViewHolder {
        public final ImageView ivimage;
        public final View root;
        public final ImageView delete_image;

        public ViewHolder(View root) {
            ivimage = (ImageView) root.findViewById(R.id.item_grida_image);
            delete_image = (ImageView) root.findViewById(R.id.delete_image);
            this.root = root;
        }
    }
}
